package ftdpc;

import ftdpc.bean.TaskAttribute;
import ftdpc.bean.TaskItem;
import ftdpc.properties.TaskConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;

import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 类描述:  任务调度业务处理
 *
 * @author 王雷
 * @version 1.0.0
 * @date 2022/9/10 0010 上午 11:50
 */
@Slf4j
public class TaskDispatchBusinessManager extends TaskDispatchBusinessProcessing {

    /**
     * 功能描述：任务调度业务处理
     *
     * @date : 2022/9/10 0010 下午 5:16
     */
    public TaskDispatchBusinessManager(ITaskItemConfigManager iTaskItemConfigManager, TaskConfig taskConfig
            , ApplicationContext applicationContext) {
        super(iTaskItemConfigManager, taskConfig, applicationContext);
    }

    /**
     * 功能描述：获取任务属性配置
     *
     * @date : 2022/9/14 0014 下午 1:07
     */
    public Map<String, TaskAttribute> giveTaskAttributeMap() {
        final Map<String, TaskAttribute> collect = giveValidList().stream().collect(Collectors.toMap(TaskItem::getTaskExecutionTarget
                , taskItem -> new TaskAttribute().putByTaskItem(taskItem)));
        return new HashMap<String, TaskAttribute>(16) {

            private static final long serialVersionUID = -1714369228460925665L;

            {
                taskAttributeMap.forEach((taskKey, taskAttribute) -> {
                    try {
                        final TaskAttribute taData = taskAttribute.deepClone();
                        final TaskAttribute taDataDb = collect.get(taData.getTaskExecutionTarget());
                        if (taDataDb != null) {
                            taData.setExecutionCycleByCron(taDataDb.getExecutionCycleByCron())
                            .setExecutionCycleByInterval(taDataDb.getExecutionCycleByInterval())
                            .setExecutionCycleByIntervalUnit(taDataDb.getExecutionCycleByIntervalUnit())
                            .setTaskExecutionTarget(taDataDb.getTaskExecutionTarget())
                            .setTaskIsAutoRun(taDataDb.getTaskIsAutoRun())
                            .setTaskIsConfig(Boolean.TRUE);
                        }
                        put(taskKey, taData);
                    }catch(Exception e){
                        log.error("处理对象：{}；出现异常；", taskAttribute, e);
                    }
                });
            }
        };
    }

//    /**
//     * 功能描述：获取任务日志信息
//     *
//     * @param taskKey 配置标识
//     * @return 日志信息
//     * @date : 2022/9/14 0014 下午 1:01
//     */
//    public String giveTaskLog(String taskKey) throws Exception {
//        final TaskAttribute taData = taskKeyIsEffective(taskAttribute.getTaskKey());
//        if (taData.getTaskIsItEffective() == 1) {
//            throw new Exception("正在执行中，请先关闭任务再进行修改");
//        }
//        taData.setTaskName(taskAttribute.getTaskName()).setExecutionCycleByCron(taskAttribute.getExecutionCycleByCron())
//                .setExecutionCycleByInterval(taskAttribute.getExecutionCycleByInterval())
//                .setExecutionCycleByIntervalUnit(taskAttribute.getExecutionCycleByIntervalUnit())
//                .setTaskExecutionTarget(taskAttribute.getTaskExecutionTarget())
//                .setTaskIsAutoRun(taskAttribute.getTaskIsAutoRun());
//        taskAttributeMap.put(taData.getTaskKey(), taData);
//    }

    /**
     * 功能描述：修改配置属性
     *
     * @param taskAttribute 任务属性对象
     * @date : 2022/9/14 0014 下午 1:01
     */
    public void updateAttribute(TaskAttribute taskAttribute) throws Exception {
        final TaskAttribute taData = taskKeyIsEffective(taskAttribute.getTaskKey());
        if (taData.getTaskIsItEffective() == 1) {
            throw new Exception("正在执行中，请先关闭任务再进行修改");
        }
        taData.setTaskName(taskAttribute.getTaskName()).setExecutionCycleByCron(taskAttribute.getExecutionCycleByCron())
                .setExecutionCycleByInterval(taskAttribute.getExecutionCycleByInterval())
                .setExecutionCycleByIntervalUnit(taskAttribute.getExecutionCycleByIntervalUnit())
                .setTaskExecutionTarget(taskAttribute.getTaskExecutionTarget())
                .setTaskIsAutoRun(taskAttribute.getTaskIsAutoRun());
        taskAttributeMap.put(taData.getTaskKey(), taData);
    }

    /**
     * 功能描述：将配置存入配置
     *
     * @param taskKey 配置标识
     * @date : 2022/9/14 0014 下午 1:01
     */
    public void putTaskAttributeToConfig(String taskKey) throws Exception {
        iTaskItemConfigManager.putValidList(taskKeyIsEffective(taskKey).toTaskItem());
    }

    /**
     * 功能描述：线程执行
     *
     * @param taskKey 配置标识
     * @date : 2022/9/14 0014 下午 1:01
     */
    public void taskStart(String taskKey) throws Exception {
        super.startTask(taskKeyIsEffective(taskKey).getTaskKey());
    }

    /**
     * 功能描述：线程停止
     *
     * @param taskKey 配置标识
     * @date : 2022/9/14 0014 下午 1:01
     */
    public void taskStopByKey(String taskKey) throws Exception {
        super.removeTask(taskKeyIsEffective(taskKey).getTaskKey());
    }

    /**
     * 功能描述：配置标识是否有效
     *
     * @param taskKey 配置标识
     * @date : 2022/9/14 0014 下午 2:15
     */
    private TaskAttribute taskKeyIsEffective(String taskKey) throws Exception {
        final TaskAttribute taskAttribute = taskAttributeMap.get(taskKey);
        if (taskAttribute == null) {
            throw new Exception("不存在的配置");
        }
        return taskAttribute;
    }
}
